package com.wlcj.manage.model;

import com.jfinal.plugin.activerecord.Model;
import com.wlcj.util.CacheKeyUtil;
import com.wlcj.util.RedisUtil;
import com.wlcj.util.SqlUtil;

public class Dict extends Model<Dict> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5633532933788672442L;
	
	public static final Dict dao = new Dict();
	
	public Dict getCacheDictById(int id) {
		String idKey = CacheKeyUtil.getDictKey(id);
		Dict d = RedisUtil.get(idKey);
		if(d==null) {
			d = findById(id);
			if(d!=null) {
				RedisUtil.set(idKey, d);
				String path = d.getStr("path");
				String pathKey = CacheKeyUtil.getDictPathKey(path);
				RedisUtil.set(pathKey, d);
			}
		}
		return d;
	}
	
	public Dict getCacheDictByPath(String path) {
		String pathKey = CacheKeyUtil.getDictPathKey(path);
		Dict d = RedisUtil.get(pathKey);
		if(d==null) {
			d = findFirst(SqlUtil.sql("dict.getDictByPath"), path);
			if(d!=null) {
				RedisUtil.set(pathKey, d);
				int id = d.getInt("id");
				String idKey = CacheKeyUtil.getDictKey(id);
				RedisUtil.set(idKey, d);
			}
		}
		return d;
	}
	
}
